package name.luojun.DBhelper.iface;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

import name.luojun.DBhelper.TableManager;
import name.luojun.DBhelper.UpgradeManager;

import org.apache.log4j.Logger;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcDaoSupport;
public abstract class UpgradeSupport<E> extends NamedParameterJdbcDaoSupport implements IUpgrade {
	protected Logger log = Logger.getLogger(UpgradeSupport.class.getName());
	protected Class<E> cls;
	private boolean deleteafter = true;
	private Resource source;
	@SuppressWarnings("unchecked")
	@Override
	public void initDao() {
		try {
			if (filePath == null || filePath.length() == 0) {
				throw new RuntimeException("property filePath should not be null!");
			}
			InputStream ins = getInputStream();
			if (ins == null) {
				log.error("upgrade resouce not found,upgrade stoped. Path:" + this.filePath);
				return;
			}
			Type genType = getClass().getGenericSuperclass();
			Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
			this.cls = (Class) params[0];
			this.cls.newInstance();
			UpgradeManager.FileUpgrade(this, this.cls, this.cls, ins, "getAll", "insert", "modify", "delete", getPrimaryKey(), getFields(),
					getRule());
			deleteFile();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	protected String filePath;
	public String getFilePath() {
		return filePath;
	}
	public void setFilePath(String filePath) {
		this.filePath = filePath;
	}
	public void deleteFile() {
		if (!deleteafter)
			return;
		File file;
		try {
			file = source.getFile();
			if (file.exists())
				file.delete();
		} catch (IOException e) {
			log.error(e.getMessage());
		}
	}
	public boolean getDeleteafter() {
		return deleteafter;
	}
	public void setDeleteafter(boolean deleteafter) {
		this.deleteafter = deleteafter;
	}
	public abstract List<E> getAll();
	public abstract void insert(E e);
	public abstract void modify(E e);
	public abstract void delete(E e);
	public abstract String getRule();
	public abstract String[] getPrimaryKey();
	public abstract String[] getFields();
	public InputStream getInputStream() {
		try {
			if (filePath.startsWith("classpath")) {
				ResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
				source = resourceLoader.getResource(filePath);
				return source.getInputStream();
			} else {
				source = new FileSystemResource(filePath);
				return source.getInputStream();
			}
		} catch (Exception e) {
			return null;
		}
	}
	public void createBackFile() {
		try {
			String path = null;
			if (source instanceof ClassPathResource) {
				path = TableManager.cleanPath(filePath);
			} else {
				path = source.getFile().getAbsolutePath();
			}
			UpgradeManager.createUpgradeFile(getAll(), path);
			log.info("upgrade file created path:" + path);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}